﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace map.PayEase
{
    public partial class ImmediateResult2 : System.Web.UI.Page
    {

        public string[] allParmaKeys = new string[] { "v_oid", "v_pmode", "v_pstatus", "v_pstring",
                        "v_md5info", "v_moneytype", "v_amount", "v_md5money","v_sign" };

        protected void Page_Load(object sender, EventArgs e)
        {
            string queryStr = HttpUtility.UrlDecode(Request.ServerVariables["QUERY_STRING"], Encoding.GetEncoding("Gb2312"));

            string id = getQueryString("id", queryStr);

            //string  urlQStr= "?v_oid=1&v_pmode=1&v_pstatus=0&v_pstring=1&v_md5info=1&v_moneytype=1&v_amount=1&v_md5money=1&v_sign=1";
            //if (Request.QueryString.HasKeys() == false)
            //{
            //    Response.Redirect(Request.Url.ToString() + urlQStr);
            //    return;
            //}
            string v_oid = getQueryString("v_oid",queryStr );
            string v_pmode = getQueryString("v_pmode",queryStr );
            string v_pstatus = getQueryString("v_pstatus",queryStr );
            string v_pstring = getQueryString("v_pstring",queryStr );
            string v_md5info = getQueryString("v_md5info",queryStr );
            string v_moneytype = getQueryString("v_moneytype",queryStr );
            string v_amount = getQueryString("v_amount",queryStr );
            string v_md5money = getQueryString("v_md5money",queryStr );
            string v_sign = getQueryString("v_sign",queryStr );

            Response.Write(queryStr+"<br/>");

            foreach (string key in allParmaKeys)
            {
                if( getQueryString(key,queryStr )==string.Empty )
                {
                    Response.Write("error");
                    return;
                }
            }

            ClassHmac2003.ClassHmac cc = new ClassHmac2003.ClassHmac();
            string md5infoText = v_oid + v_pstatus + v_pstring + v_pmode;
            string md5info = cc.hex_hmac_md5(md5infoText, PayEaseUtil.md5_key);
            Response.Write("md5info:");
            Response.Write(md5info);
            Response.Write("<br>");

            string md5moneyText = v_amount + v_moneytype ;
            string md5money = cc.hex_hmac_md5(md5moneyText, PayEaseUtil.md5_key);
            Response.Write("md5money:");
            Response.Write(md5money);
            Response.Write("<br>");

            if (md5info != v_md5info || md5money != v_md5money)
            {
                Response.Write("error");
            }
            else
            {
                Response.Write("sent");
            }

            //RSA验证
            string  strSource = v_oid + v_pstatus + v_amount + v_moneytype ;

            RSA_MD5 rsa = new RSA_MD5();
            int d = rsa.PublicVerifyMD5(Server.MapPath ("Public1024.key"), v_sign, strSource);
            Response.Write(d);
        }

        /// <summary>
        /// 获取变量值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="allQueryText"></param>
        /// <returns></returns>
        private string getQueryString(string key,string allQueryText)
        {
            string result = string.Empty;
            int start = allQueryText.IndexOf(key+"=");
            if (start >= 0)
            {
                int end = allQueryText.IndexOf("&", start);
                if (end > (start + key.Length + 1))
                {
                    result = allQueryText.Substring(start + key.Length + 1, end - start - key.Length - 1);
                }
                else
                {
                    if (end == -1)
                    {
                        result = allQueryText.Substring(start + key.Length + 1, allQueryText.Length  -start - key.Length - 1);
                    }
                }
            }
            return result;
        }
    }
}
